# -*-coding:utf-8-*-
from typing import Union

# 取最大值
def my_max(arg: Union[int, float], *args: Union[int, float], key=None) -> Union[int, float]:
    _list = (arg,) + args
    if key is not None:
        _list = list((x, key(x)) for x in _list)
        # 等同于
        # _list = list(map(lambda x: (x, key(x)), _list))
    _list.sort(key=lambda x: x[1], reverse=True)
    return _list[0][0]

print(my_max(1, 2, 3, -4, key=lambda x: x**2))


# 实现回调函数
class TestListener(object):
    def __init__(self):
        self.string = 'call listener'
        self.listener = []

    def addlistener(self, listener):
        self.listener.append(listener)

    def click(self):
        for l in self.listener:
            l(self.string)

def echo(s):
    print(s)
test = TestListener()
test.addlistener(lambda s: print(s))
test.addlistener(echo)
test.click()